package com.cs.controller;

import com.alibaba.excel.EasyExcel;
import com.cs.entity.Department;
import com.cs.resultvo.Result;
import com.cs.service.DepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.MultipartFilter;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Date;

@RestController
@Api("deptTestSwagger2")
public class DeptController {


    @Autowired
    private DepartmentService departmentService;


    @ApiOperation("add")
    @PostMapping("/add")
    public String test1(@RequestBody DeptBo dept){
        Department department = new Department();
        BeanUtils.copyProperties(dept,department);
        department.setCreateTime(new Date(dept.getCreateTime()));
        departmentService.save(department);
        return "ok";
    }

    @ApiOperation("test")
    @GetMapping("/test")
    public String test1(){
        System.out.println(new Date().getTime());
        return "ok";
    }

    @ApiOperation("importDept")
    @PostMapping("/importDept")
    public Result shangchuan(MultipartFile file){
        departmentService.importDeptData(file);
        return Result.ok();
    }

    @ApiOperation("downLoad")
    @GetMapping("/downLoad")
    public Result downLoad(HttpServletResponse response){

        try {
            departmentService.exportDeptData(response);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return Result.ok();
    }


}
